//扫注册码功能封装代码----------------------
(function(window,factroy,plug){
     return factroy(jQuery,plug)
})(this,function($,plug){
     var DEFALUT={
          events:"click",
          scanBtn:"body",//扫码按钮
     };

     $.fn[plug]=function(options){
          var self=this;
          var snInput=$("#sn_put");
          //对象扩展函数调用-------------------
          $.extend(this,DEFALUT,options);
          //点击设备码调用弹出框进行-------------
          $(this.scanBtn).on(this.events,function(){
               $("#choose_sn").removeClass("hide");
          });

          //手动输入事件
          $("#sn_input_hand").on("touchstart",function(){
               $("#hand_sn").removeClass("hide").prev().addClass("hide");
          });



          //扫码输入事件
          $("#scan").on("touchstart",function(){
               AnroidWebActivity.scanbarcode(scancallback);
          });


          //关闭弹窗事件
          $(".closed_btn").on("touchstart",function(){
               $(this).parents(".model").addClass("hide");
          });

          //确认sn输入码
          $("#sn_sure_btn").on("touchstart",function(){
               var valueSn=snInput.val();
               if(valueSn.length==0){
                    snInput.addClass("error");
                    snInput.attr("placeholder","注册码不能为空");
               }else{
                    getPublic(valueSn);
                    $(".scanning").html('<span class="txt_block"><em>注册码:</em><em>'+valueSn+'</em></p>');
                    $("#hand_sn").addClass("hide");
               }
          });

          snInput.focus(function(){
               $(this).attr("placeholder","输入注册码");
               $(this).removeClass("error");
          })
     }

},"scanCode");


//扫码参数配置--------------------------------------------------
$("#sn-code").scanCode({
     events:"touchstart",
     //触发按钮
     scanBtn:"#sn-code"
});


$("#agree_link").on("touchstart",function(){
   $(this).attr("href",baseAll.getUrl(window.location.href,"agreement.html"));
   window.location.href=$(this).attr("href");
});

//扫码功能
function scancallback(result){
     $("#choose_sn").addClass("hide");
     $(".scanning").html('<span class="txt_block"><em>注册码:</em><em>'+result+'</em></p>');
     $.ajax({
          type:"get",
          url:oPublic.basePath()+"/s/bike/owner/publickey",
          dataType:"json",
          data:{
               invitationCode:result
          },
          success:function(data){
               if(data.opStatus==0){
                    var modulus=data.data.modulus;
                    var public_exponent=data.data.publicExponent;
                    localStorage.setItem("public_key",modulus);//公钥
                    localStorage.setItem("mi_key",public_exponent);//秘钥
               }else{
                    oPublic.pubilcModal(data.msgDesc);
               }
          },
          error:function(){
               oPublic.pubilcModal("网络连接失败");
          }
     })
}


//手动输入获取
function getPublic(result){
     $.ajax({
          type:"get",
          url:oPublic.basePath()+"/s/bike/owner/publickey",
          dataType:"json",
          data:{
               invitationCode:result
          },
          success:function(data){
               if(data.opStatus==0){
                    var modulus=data.data.modulus;
                    var public_exponent=data.data.publicExponent;
                    console.log(modulus);
                    console.log(public_exponent);
                    localStorage.setItem("public_key",modulus);//公钥
                    localStorage.setItem("mi_key",public_exponent);//秘钥
               }else{
                    $(".loading_model").addClass("hide");
                    oPublic.pubilcModal(data.msgDesc);
               }
          },
          error:function(){
               console.log("data is faliure...");
          }
     })
}